﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;

namespace RFID信息初始化工作台
{
    public partial class frmConfig : Form
    {
        string oldServer = "";
        string oldUser = "";
        string oldPwd = "";

        private string _sEng01 = "DB Server Name or IP:";
        private string _sEng02 = "DB User Name:";
        private string _sEng03 = "DB Password:";

        public frmConfig()
        {
            InitializeComponent();
            if(Program._Language == Program.Language.Eng)
            {
                label1.Text = _sEng01;
                label2.Text = _sEng02;
                label3.Text = _sEng03;
                btn_Save.Text = "Save";
                btn_Test.Text = "Test";
            }
        }

        private void LoadDBConfig()
        {
            if (!File.Exists(Commons.sAppFile))
            {
                if (Program._Language == Program.Language.Chs)
                    MessageBox.Show("读取数据库配置文件失败!");
                else
                    MessageBox.Show("Failed to read database configuration file");
            }
            else
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(Commons.sAppFile);
                XmlNode xn = xml.GetElementsByTagName("add")[0];
                string str = xn.Attributes["connectionString"].Value;
                string server = str.Substring(str.LastIndexOf("data source=") + 12);
                server = server.Substring(0, server.IndexOf(";"));
                txt_Server.Text = server;

                string uid = str.Substring(str.LastIndexOf("user id=") + 8);
                uid = uid.Substring(0, uid.IndexOf(";"));
                txt_User.Text = uid;

                string pwd = str.Substring(str.LastIndexOf("password=") + 9);
                pwd = pwd.Substring(0, pwd.IndexOf(";"));
                txt_Pwd.Text = pwd;


                oldServer = "data source=" + server;
                oldUser = "user id=" + uid;
                oldPwd = "password=" + pwd;
            }
        }


        private void frmConfig_Load(object sender, EventArgs e)
        {
            LoadDBConfig();
        }

        private void btn_Test_Click(object sender, EventArgs e)
        {
            //YDRFIDEntities conn = new YDRFIDEntities();
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
            conn.ConnectionString = "server=" + txt_Server.Text + ";uid=" + txt_User.Text + ";pwd=" + txt_Pwd.Text;
            try
            {
                conn.Open();
                conn.Close();
                if (Program._Language == Program.Language.Chs)
                {
                    MessageBox.Show("数据库连接成功!");
                }
                else
                    MessageBox.Show("Database connection succeeded");
            }
            catch {
                if (Program._Language == Program.Language.Chs)
                    MessageBox.Show("数据库连接失败!");
                else
                    MessageBox.Show("Database connection failed");
            }
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader(Commons.sAppFile, Encoding.GetEncoding("gbk"));
            List<string> list = new List<string>();
            while(!sr.EndOfStream)
            {
                list.Add(sr.ReadLine());
            }
            sr.Close();
            StreamWriter sw = new StreamWriter(Commons.sAppFile, false, Encoding.GetEncoding("gbk"));
            foreach(var s in list)
            {
                string sn = s;

                sn = s.Replace(oldServer, "data source=" + txt_Server.Text);
                sn = sn.Replace(oldPwd, "password=" + txt_Pwd.Text);
                sn = sn.Replace(oldUser, "user id=" + txt_User.Text);
                sw.WriteLine(sn);
            }
            sw.Close();
            if (Program._Language == Program.Language.Chs)
                MessageBox.Show("保存后请重新启动软件!");
            else
                MessageBox.Show("Please restart the software after saving");
        }
    }
}
